<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
     public function _initialize(){
        if(!session('?adminname')){
           $this->error('当前用户没有权限','../Login/login');
           exit;
        }
    }
    public function index(){
        $this->display();
    }
Public function verify(){
    // 导入Image类库
    import("ORG.Util.Image");
    Image::buildImageVerify();
}
    public function userlist(){
        $usersmodel=M('users');
        $users=$usersmodel->order('uid desc')->select();
        $this->users=$users;
        $this->display();
    }
     public function groupslist(){
        $groupsmodel=M('groups');
        $groups=$groupsmodel->order('gid desc')->select();
        $this->groups=$groups;
        $this->display();
    }
    public function practiceslist(){
        $practicesmodel=M('practices');
        $practices=$practicesmodel->order('pid desc')->select();
        $this->practices=$practices;
        $this->display();
    }
    public function memberslist(){
        $practicesmodel=M('members');
        $practices=$practicesmodel->query('select * from members where gid ='.$_GET['id']);
        $this->practices=$practices;
        $this->display();
    }
    public function stupraclist(){
        $practicesmodel=M('stuprac');
        $stupracmodel=M('practices');
        $practices=$practicesmodel->query('select * from stuprac where pid ='.$_GET['id']);
        $stuprac=$stupracmodel->query('select pname from practices where pid ='.$_GET['id']);
        $this->practices=$practices;
        $this->stuprac=$stuprac;
        $this->display();
    }
     public function newslist(){
        $usersmodel=M('news');
        $users=$usersmodel->order('nid desc')->select();
        $this->users=$users;
        $this->display();
    }
    public function del($id){
        //生成一个
        $usersModel=M('users');
        if($usersModel->delete($id))
        {
          $this->success('删除成功');
        }else
        {
           $this->error('删除失败');
        }
    }
    public function delgroup($id){
        //生成一个
        $usersModel=M('groups');
        if($usersModel->delete($id))
        {
          $this->success('删除成功');
        }else
        {
           $this->error('删除失败');
        }
    }
    public function delpractice($id){
        //生成一个
        $usersModel=M('practices');
        if($usersModel->delete($id))
        {
          $this->success('删除成功');
        }else
        {
           $this->error('删除失败');
        }
    }
     public function delmember($id){
        //生成一个
        $usersModel=M('members');
        if($usersModel->delete($id))
        {
          $this->success('删除成功');
        }else
        {
           $this->error('删除失败');
        }
    }
    public function delnew($id){
        //生成一个
        $usersModel=M('news');
        if($usersModel->delete($id))
        {
          $this->success('删除成功');
        }else
        {
           $this->error('删除失败');
        }
    }
    function upload(){
           $upload = new \Think\Upload();// 实例化上传类
           $upload->maxSize = 3145728 ;// 设置附件上传大小
           $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
           $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
            //$upload->savePath = ''; // 设置附件上传（子）目录
            // 上传文件
            $upload->autoSub=false;
            $info = $upload->upload();
            if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
            }else{// 上传成功
            //$this->success('上传成功！');
                return $info;
            }
    }

    function img(){
        //if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
    $photoinfo=$this->upload();//将临时文件保存到指定位置
    //$userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
    //var_dump($photoinfo);
    $filePath='./Public/Uploads/photo/';
    $oldFileName=$photoinfo['photo']['savename'];
    $oldFileFullName=$filePath."\\".$oldFileName;
    $newFileName='thumb_'.$oldFileName;
    $newFileFullName=$filePath."\\".$newFileName;
    // $img=$photoinfo['photo']['rootPath'].'\\'.$photoinfo['photo']['savename'];
    $image=new \Think \Image();
    $image->open($oldFileFullName);
    $image->thumb(100,100)->save($newFileFullName);
    return $newFileName;
 // }

    }//图像处理海曙
function img2(){
        //if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
    $photoinfo=$this->upload();//将临时文件保存到指定位置
    //$userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
    //var_dump($photoinfo);
    $filePath='./Public/Uploads/photo/';
    $oldFileName=$photoinfo['pfile']['savename'];
    $oldFileFullName=$filePath."\\".$oldFileName;
    $newFileName='thumb_'.$oldFileName;
    $newFileFullName=$filePath."\\".$newFileName;
    // $img=$photoinfo['photo']['rootPath'].'\\'.$photoinfo['photo']['savename'];
    $image=new \Think \Image();
    $image->open($oldFileFullName);
    $image->thumb(100,100)->save($newFileFullName);
    return $newFileName;
 // }

    }//图像处理海曙
    public function adduser(){
        
        $userModel=D('users');
        if($userModel->create()){
            if($_FILES['photo']['error']==0)
          {
            $fileName=$this->img();
            $userModel->photo=$fileName;
          }
            if ($userModel->add()) {
             $this->success('添加用户成功','userlist');
            }
            else{
              $this->error('添加用户失败');
                }
            }
            else{$this->error($userModel->getError());
            }
            }
       public function addgroups(){
        $groupModel=D('groups');
        if($groupModel->create()){
            if($_FILES['photo']['error']==0)
          {
            $fileName=$this->img();
            $groupModel->photo=$fileName;
          }
            if ($groupModel->add()) {
             $this->success('添加社团成功','groupslist');
            }
            else{
              $this->error('添加社团失败');
                }
            }
            else{$this->error($groupModel->getError());
            }
            }  
        public function addpractices(){
        $practiceModel=D('practices');
        if($practiceModel->create()){
            if($_FILES['pfile']['error']==0)
          {
            $fileName=$this->img2();
            $practiceModel->pfile=$fileName;
          }
            if ($practiceModel->add()) {
             $this->success('添加社团活动成功','practiceslist');
            }
            else{
              $this->error('添加社团活动失败');
                }
            }
            else{$this->error($practiceModel->getError());
            }
            }   
            public function addmember(){
        
        $practiceModel=M('members');
        if($practiceModel->create()){
            if ($practiceModel->add()) {
             $this->success('添加社团成员成功','groupslist');
            }
            else{
              $this->error('添加社团成员失败');
                }
            }
            }   
        public function addguser(){
        $practicesmodel=M('users');
        $practices=$practicesmodel->query('select stunum from users');
        $this->practices=$practices;
        $this->display();
    }
    public function addpuser(){
        $practicesmodel=M('users');
        $practices=$practicesmodel->query('select stunum from users');
        $this->practices=$practices;
        $this->display();
    }
    public function addpmember(){
        
        $practiceModel=M('stuprac');
        if($practiceModel->create()){
            if ($practiceModel->add()) {
             $this->success('添加参与者成功','practiceslist');
            }
            else{
              $this->error('添加参与者失败');
                }
            }
            } 
         public function addnews(){
        
        $userModel=D('news');
        if($userModel->create()){
            if($_FILES['photo']['error']==0)
          {
            $fileName=$this->img();
            $userModel->photo=$fileName;
          }
            if ($userModel->add()) {
             $this->success('添加新闻成功','newslist');
            }
            else{
              $this->error('添加新闻失败');
                }
            }
            else{$this->error($userModel->getError());
            }
            }
      public function update($id){
        $usersModel=M('users');
        $this->user=$usersModel->find($id);
        $this->display();
        //将查询到的用户信息传递给模板 update.html
    }
    public function doupdate(){
        //获取updateUserInfo提交过来的数据，写入并修改数据库
    //获取参数方法 $_POST I(post.+字段名)  例$data=$_POST    $data=I(post,)
    // create()  利用$_POST数组创建一个对象  
    $userModel=D('users');
    $userModel->create();

 // if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
   // $photoinfo=$this->upload();//将临时文件保存到指定位置
   // $userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
  //}
    if ($userModel->save()) {
     $this->success('修改用户成功','userlist');
    }
    else{
      $this->error('修改用户失败');
        }
    }


    public function updategroup($id){
        $groupsModel=M('groups');
        $this->group=$groupsModel->find($id);
        $this->display();
        //将查询到的用户信息传递给模板 update.html
    }
    public function doupdategroup(){
        //获取updateUserInfo提交过来的数据，写入并修改数据库
    //获取参数方法 $_POST I(post.+字段名)  例$data=$_POST    $data=I(post,)
    // create()  利用$_POST数组创建一个对象  
    $groupModel=D('groups');
    $groupModel->create();

 // if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
   // $photoinfo=$this->upload();//将临时文件保存到指定位置
   // $userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
  //}
    if ($groupModel->save()) {
     $this->success('修改社团成功','groupslist');
    }
    else{
      $this->error('修改社团失败');
        }
    }
    public function updatepractice($id){
        $practicesModel=M('practices');
        $this->practice=$practicesModel->find($id);
        $this->display();
        //将查询到的用户信息传递给模板 update.html
    }
    public function doupdatepractice(){
        //获取updateUserInfo提交过来的数据，写入并修改数据库
    //获取参数方法 $_POST I(post.+字段名)  例$data=$_POST    $data=I(post,)
    // create()  利用$_POST数组创建一个对象  
    $practiceModel=D('practices');
    $practiceModel->create();

 // if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
   // $photoinfo=$this->upload();//将临时文件保存到指定位置
   // $userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
  //}
    if ($practiceModel->save()) {
     $this->success('修改社团成功','practiceslist');
    }
    else{
      $this->error('修改社团失败');
        }
    }
 public function updatenew($id){
        $practicesModel=M('news');
        $this->new=$practicesModel->find($id);
        $this->display();
        //将查询到的用户信息传递给模板 update.html
    }
    public function doupdatenew(){
        //获取updateUserInfo提交过来的数据，写入并修改数据库
    //获取参数方法 $_POST I(post.+字段名)  例$data=$_POST    $data=I(post,)
    // create()  利用$_POST数组创建一个对象  
    $practiceModel=D('news');
    $practiceModel->create();

 // if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
   // $photoinfo=$this->upload();//将临时文件保存到指定位置
   // $userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
  //}
    if ($practiceModel->save()) {
     $this->success('修改社团成功','newslist');
    }
    else{
      $this->error('修改社团失败');
        }
    }
    public function setjy($id){
        //生成一个user表对应的模型实例usersModel
        $usersModel=M('users');
        $data['state']=1;
        if($usersModel->where("uid=$id")->save($data)){
        //update users set isstar=1 where id=$id
         $this->success('禁言成功');
    }else
        {
           $this->error('禁言失败');
        }

        //$usersModel->isstar=1;
    }
    public function deljy($id){
        $usersModel=M('users');
        $usersModel->state=0;
        $usersModel->uid=$id;
        if ($usersModel->save()) {
            $this->success('解禁成功');
    }else
        {
           $this->error('解禁失败');
        }
    }
    public function setgjy($id){
        //生成一个user表对应的模型实例usersModel
        $usersModel=M('groups');
        $data['state']=1;
        
        if($usersModel->where("gid=$id")->save($data)){
        //update users set isstar=1 where id=$id
         $this->success('禁言成功');
    }else
        {
           $this->error('禁言失败');
        }

        //$usersModel->isstar=1;
    }
    public function delgjy($id){
        $usersModel=M('groups');
        $usersModel->state=0;
        $usersModel->gid=$id;
        if ($usersModel->save()) {
            $this->success('解禁成功');
    }else
        {
           $this->error('解禁失败');
        }
    }

    public function setpjy($id){
        //生成一个user表对应的模型实例usersModel
        $usersModel=M('practices');
        $data['state']=1;
        
        if($usersModel->where("pid=$id")->save($data)){
        //update users set isstar=1 where id=$id
         $this->success('允许发布成功');
    }else
        {
           $this->error('允许发布失败');
        }

        //$usersModel->isstar=1;
    }
    public function delpjy($id){
        $usersModel=M('practices');
        $usersModel->state=0;
        $usersModel->pid=$id;
        if ($usersModel->save()) {
            $this->success('禁止发布成功');
    }else
        {
           $this->error('禁止发布失败');
        }
    }

    public function setmjy($id){
        //生成一个user表对应的模型实例usersModel
        $usersModel=M('members');
        $data['state']=1;
        
        if($usersModel->where("mid=$id")->save($data)){
        //update users set isstar=1 where id=$id
         $this->success('允许参加成功');
    }else
        {
           $this->error('允许参加失败');
        }

        //$usersModel->isstar=1;
    }
    public function delmjy($id){
        $usersModel=M('members');
        $usersModel->state=0;
        $usersModel->mid=$id;
        if ($usersModel->save()) {
            $this->success('禁止参加成功');
    }else
        {
           $this->error('禁止参加失败');
        }
    }
    public function delsetmjy($id){
        $usersModel=M('members');
        $usersModel->state=2;
        $usersModel->mid=$id;
        if ($usersModel->save()) {
            $this->success('设立负责人成功');
    }else
        {
           $this->error('设立负责人失败');
        }
    }
    public function setsjy($id){
        //生成一个user表对应的模型实例usersModel
        $usersModel=M('stuprac');
        $data['state']=1;
        
        if($usersModel->where("sid=$id")->save($data)){
        //update users set isstar=1 where id=$id
         $this->success('允许参加成功');
    }else
        {
           $this->error('允许参加失败');
        }

        //$usersModel->isstar=1;
    }
    public function delsjy($id){
        $usersModel=M('stuprac');
        $usersModel->state=0;
        $usersModel->sid=$id;
        if ($usersModel->save()) {
            $this->success('禁止参加成功');
    }else
        {
           $this->error('禁止参加失败');
        }
    }
    public function setnjy($id){
        //生成一个user表对应的模型实例usersModel
        $usersModel=M('news');
        $data['state']=1;
        
        if($usersModel->where("nid=$id")->save($data)){
        //update users set isstar=1 where id=$id
         $this->success('发布成功');
    }else
        {
           $this->error('发布失败');
        }

        //$usersModel->isstar=1;
    }
    public function delnjy($id){
        $usersModel=M('news');
        $usersModel->state=0;
        $usersModel->nid=$id;
        if ($usersModel->save()) {
            $this->success('禁止发布成功');
    }else
        {
           $this->error('禁止发布失败');
        }
    }
}